#!/bin/bash

# 用于安装JDK开发环境和常用jar包

# 卸载已经安装的、系统自带的OpenJDK
echo ">>>>卸载系统自带的OpenJDK"
rpm -e --nodeps jdk-1.6.0_45-fcs 2> /dev/null
rpm -e --nodeps java-1.4.2-gcj-compat-1.4.2.0-40jpp.115 2> /dev/null
rpm -e --nodeps --allmatches libgcj-4.1.2-44.el5 2> /dev/null
rpm -e --nodeps --allmatches libgcj-4.1.2-50.el5 2> /dev/null
rpm -e --nodeps java-1.7.0-openjdk 2> /dev/null
rpm -e --nodeps java-1.7.0-openjdk-headless 2> /dev/null
rpm -e --nodeps java-1.8.0-openjdk 2> /dev/null
rpm -e --nodeps java-1.8.0-openjdk-headless 2> /dev/null

# 安装Java环境
echo ">>>>安装Oracle JDK 1.8.0_u74"
rpm -ivh ./jdk-8u74-linux-x64.rpm 2> /dev/null > /dev/null

# 安装jar包
echo ">>>>拷贝第三方常用JAR包"
if ! [ -d /usr/java/libs ]; then
	mkdir -p /usr/java/libs
fi
cp -r ./libs/* /usr/java/libs/

# 配置系统Java环境
echo ">>>>配置系统Java环境变量"
if [ "$(grep 'JAVA_HOME' /etc/profile)" != "" ]; then
	echo "JAVA_HOME exists"
else
	cat >> /etc/profile << EOF

# java path added by zw 2017/8/15
export JAVA_HOME=/usr/java/default
export JRE_HOME=/usr/java/default/jre
export CLASSPATH=.:\$JAVA_HOME/lib/tools.jar:\$JAVA_HOME/lib/dt.jar
export PATH=\$PATH:\$JAVA_HOME/bin:\$JRE_HOME/bin
EOF
	source /etc/profile
fi

